package dao.impl;



import dao.BaseDao;
import dao.petDao;
import entity.Pet;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class petDaoImpl extends BaseDao implements petDao {
    /**
     * 查询所有
     * @return
     */
    public List<Pet> SelectPetinAll() {
        List<Pet> list=new ArrayList<>();
        String sql="select * from pet";
        Object[] obj=null;
        ResultSet rs=null;
        try {
            rs=super.executeQuery(sql,obj);
            while (rs.next()){
                Pet pet=new Pet();
                pet.setId(rs.getInt(1));
                pet.setName(rs.getString(2));
                pet.setBreed(rs.getString(3));
                pet.setSex(rs.getString(4));
                pet.setBirthday(rs.getString(5));
                pet.setDescRipTion(rs.getString(6));
                list.add(pet);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            try {
                if (null!=rs){
                    rs.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return list;
    }

    /**
     * 品种查询
     * @param Breed
     * @return
     */
    public List<Pet> SelectPetinBreed(String Breed) {
        List<Pet> list=new ArrayList<>();
        String sql="select * from pet where petBreed=?";
        Object[] obj={Breed};
        ResultSet rs=null;

        try {
            rs=super.executeQuery(sql,obj);
            while (rs.next()){
                Pet pet=new Pet();
                pet.setId(rs.getInt(1));
                pet.setName(rs.getString(2));
                pet.setBreed(rs.getString(3));
                pet.setSex(rs.getString(4));
                pet.setBirthday(rs.getString(5));
                pet.setDescRipTion(rs.getString(6));
                list.add(pet);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            try {
                if (null!=rs){
                    rs.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return list;
    }

    /**
     * 新增
     * @param pet
     * @return
     */
    public int addPet(Pet pet) {
        String sql="INSERT into pet(petName,petBreed,petSex,birthday,description) VALUES(?,?,?,?,?)";
        Object[] obj={pet.getName(),pet.getBreed(),pet.getSex(),pet.getBirthday(),pet.getDescRipTion()};
        int count=0;
        try {
            count=super.executeUpdate(sql,obj);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return count;
    }
}
